<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true">添加分类</el-button>
      </div>
<!-- 分类数据的表格 -->
       <el-table style="width: 100%" border stripe :data="cateList">
        <el-table-column label="序号" width="100" type="index"></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column >
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作">
        <template v-slot='scope'>
        <el-button type="primary" size="mini" @click='updateFn(scope.row)'>修改</el-button>
          <el-button type="danger" size="mini" @click="removeFn(scope.row.id)">删除</el-button>
        </template>
        </el-table-column>
      </el-table>
    </el-card>

  <!-- 打开添加分类的弹窗 -->
<el-dialog title="添加文章分类" :visible.sync="addVisible" width="35%" @close='dialogCloseFn'>
   <!-- 添加的表单 -->
<el-form :model="addForm" :rules="addRules" ref="addRef" label-width="80px">
  <el-form-item label="分类名称" prop="cate_name">
    <el-input v-model="addForm.cate_name" minlength="1" maxlength="10"></el-input>
  </el-form-item>
  <el-form-item label="分类别名" prop="cate_alias">
    <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15"></el-input>
  </el-form-item>
</el-form>
    <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="cancelFn">取 消</el-button>
        <el-button size="mini" type="primary" @click="addFn">确定</el-button>
    </span>
</el-dialog>
  </div>
</template>

<script>
import { getArtCateListAPI, addArtCateAPI, updateArtCateAPT, delArtCateAPI } from '@/api/index' // 获取文章分类的接口 添加文章的接口  修改文章的接口  删除文章的接口
export default {
  name: 'ArtCate',
  data () {
    return {
      cateList: [], // 文章列表数据
      addVisible: false, // 控制增加文字分类的弹窗
      addForm: { // 添加表单的数据对象
        cate_name: '',
        cate_alias: ''
      },
      addRules: { // 添加表单的验证规则对象
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      },
      isEdit: false, // true为编辑修改文章状态,false新增状态
      editId: '' // 获取当前文章的id
    }
  },
  created () {
    this.initCateListFn()// 获取文章分类的方法
  },
  methods: {
    async initCateListFn () { // 获取文章分类的方法
      const { data } = await getArtCateListAPI()
      this.cateList = data.data
    },
    cancelFn () { // 取消添加文章
      this.addVisible = false
    },
    addFn () { // 添加文章按钮
      this.$refs.addRef.validate(async val => {
        if (val) { // 兜底校验成功
          if (this.isEdit) { // 说明要编辑文文章
          // 添加id
            this.addForm.id = this.editId
            const { data } = await updateArtCateAPT(this.addForm)
            if (data.code === 0) {
              this.$message.success(data.message)
              this.initCateListFn()// 获取文章分类的方法
            } else {
              this.$message.error(data.message)
            }
          } else { // 不然就是添加文章
            this.isEdit = true // 状态变成添加文章
            this.editId = '' // 把之前的文章id清除
            try {
              const { data } = await addArtCateAPI(this.addForm)
              console.log(1)
              if (data.code === 0) {
                this.$message.success(data.message)
                this.initCateListFn()// 获取文章分类的方法
              } else {
                this.$message.error(data.message)
              }
            } catch (err) {
              console.log(err, 'err')
            }
            this.addVisible = false
          }
        } else {
          return false
        }
      })
    },
    dialogCloseFn () { // 添加文章分类弹框关闭适合的回调
      this.$refs.addRef.resetFields()
    },
    updateFn (obj) { // 点击修改文章的方法
      this.addVisible = true // 弹窗显示
      this.isEdit = true // 弹窗变成修改状态
      this.editId = obj.id // 辅助当前文章的id
      this.$nextTick(() => {
        this.addForm.cate_name = obj.cate_name
        this.addForm.cate_alias = obj.cate_alias
      })
    },
    removeFn (id) { // 删除文章的方法
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await delArtCateAPI(id)
        this.initCateListFn()// 获取文章分类的方法
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}

</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
